package com.neuedu.oasystem.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.neuedu.oasystem.dao.IRoleDao;

import com.neuedu.oasystem.dao.impl.RoleDaoImpl;
import com.neuedu.oasystem.entity.Role;
import com.neuedu.oasystem.service.RoleService;
import com.neuedu.oasystem.util.DBHelper;



public class RoleServiceImpl implements RoleService {
	private IRoleDao roleDao;
	public RoleServiceImpl() {
		super();
		this.roleDao = new RoleDaoImpl();
	}
	

	@Override
	public boolean add(Role param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			roleDao.setConnection(conn);
			boolean ret = roleDao.add(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
			
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}

			}
			e.printStackTrace();
		} finally {
			System.out.println("添加成功");
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean edit(Role param) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			roleDao.setConnection(conn);
			boolean ret = roleDao.edit(param);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			System.out.println("修改成功");
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public boolean deleteById(int id) {
		Connection conn = DBHelper.openConnection();
		try {
			conn.setAutoCommit(false);
			roleDao.setConnection(conn);
			boolean ret = roleDao.deleteById(id);
			conn.commit();
			conn.setAutoCommit(true);
			return ret;
		} catch (SQLException e) {
			if (conn != null) {
				try {
					conn.rollback();
					conn.setAutoCommit(true);
				} catch (Exception e2) {
				}
			}
			e.printStackTrace();
		} finally {
			System.out.println("删除成功");
			DBHelper.closeAll(null, null, conn);
		}
		return false;
	}

	@Override
	public Role findById(int id) {
		
		return null;
	}

	@Override
	public List<Role> findAll( ) {
		Connection conn = DBHelper.openConnection();
		try {
			roleDao.setConnection(conn);
			List<Role> list = roleDao.findAll();
			return list;
		} catch (Exception e) {
		} finally {
			DBHelper.closeAll(null, null, conn);
		}

		return null;
	}

	@Override
	public List<Role> findByExample(Role param) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Role> findByPager(int page, int size) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByIds(int[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int addAll(List<Role> useparamrs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int editAll(List<Role> param) {
		// TODO Auto-generated method stub
		return 0;
	}

}
